
import 'package:audioplayers/audioplayers.dart';
import 'package:flame_audio/flame_audio.dart';
import '../config/AppConst.dart';
import 'SpUtils.dart';

/**
 * 本地存储工具
 */
class AudioUtils {
  AudioPlayer? audioPlayer;

  //构造方法
  AudioUtils._() {
    init();
  }

  static AudioUtils? _instance;

  AudioUtils._pre() {
    this.audioPlayer = AudioPlayer();
  }

  ///预初始化，防止在使用get时，prefs还未完成初始化
  static Future<AudioUtils?> preInit() async {
    if (_instance == null) {
      _instance = AudioUtils._pre();
    }
    return _instance;
  }

  static AudioUtils? getInstance() {
    if (_instance == null) {
      _instance = AudioUtils._();
    }
    return _instance;
  }

  void init() async {
    FlameAudio.bgm.initialize();
    if (audioPlayer == null) {
      audioPlayer = await AudioPlayer();
    }
  }

  void play(String path) {
    FlameAudio.play(path);
    //  FlameAudio.play('travel_complete.mp3');
  }

  void play2(String path) {
    bool? isvoice = SpUtils.getInstance()?.getBool2(AppConst.voice);
    if (isvoice != null) {
      if (isvoice) {
        FlameAudio.play(path);
        // audioPlayer?.play(AssetSource("audio/$path"));
      }
    } else {
      FlameAudio.play(path);
      // audioPlayer?.play(AssetSource("audio/$path"));
    }
  }

/**
 * 循环播放
 */
  void playLood(String path) {
    FlameAudio.bgm.play(
      path,
    );
  }

  void playLood2(String path, double volume) {
    FlameAudio.bgm.play(path, volume: volume);
  }

  void pause() {
    //  audioPlayer?.stop();
    FlameAudio.bgm.pause();
  }

  void resume() {
    FlameAudio.bgm.resume();
    // audioPlayer?.resume();
  }

  void release() {
    print("暂停");
    FlameAudio.bgm.pause();
    // audioPlayer?.release();
  }

  void stopHomeBGm() {
    audioPlayer?.pause();
  }

/**
 * 播放/关闭首页背景音乐
 */
  bool isFirsitDay = false;
  void startOrStopBgm() {
    //暂停其他音乐
    pause();
    bool? _isBGvoice = SpUtils.getInstance()?.getBool2(AppConst.bgvoice);
    if (_isBGvoice != null) {
      if (_isBGvoice) {
        _plasyBgm();
        //  playLood("orter_bgm.mp3");
      } else {
        audioPlayer?.pause();
      }
    } else {
      _plasyBgm();
    }
  }

  void _plasyBgm() {
    audioPlayer?.pause();
    if (isFirsitDay) {
      audioPlayer?.resume();
    } else {
      isFirsitDay = true;
      audioPlayer?.play(AssetSource("audio/orter_bgm.mp3"), volume: 0.7);
    }

    audioPlayer?.onPlayerComplete.listen((event) {
      audioPlayer?.play(AssetSource("audio/orter_bgm.mp3"), volume: 0.7);
    });
  }
}



//为了公司的电脑能用

// class AudioUtils {


//   //构造方法
//   AudioUtils._() {
//     init();
//   }

//   static AudioUtils? _instance;

//   AudioUtils._pre() {
   
//   }

//   ///预初始化，防止在使用get时，prefs还未完成初始化
//   static Future<AudioUtils?> preInit() async {
//     if (_instance == null) {
//       _instance = AudioUtils._pre();
//     }
//     return _instance;
//   }

//   static AudioUtils? getInstance() {
//     if (_instance == null) {
//       _instance = AudioUtils._();
//     }
//     return _instance;
//   }

//   void init() async {
 
//   }

//   void play(String path) {
  
//     //  FlameAudio.play('travel_complete.mp3');
//   }

//   void play2(String path) {
    
//   }

// /**
//  * 循环播放
//  */
//   void playLood(String path) {
  
//   }

//   void playLood2(String path, double volume) {
   
//   }

//   void pause() {

//   }

//   void resume() {
  
//   }

//   void release() {

//     // audioPlayer?.release();
//   }

//   void stopHomeBGm() {
 
//   }

// /**
//  * 播放/关闭首页背景音乐
//  */
//   bool isFirsitDay = false;
//   void startOrStopBgm() {
    
//   }

//   void _plasyBgm() {
   
//   }
// }
